package com.blockchain.console.servlets.content.banner;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.blockchain.common.enums.SbType;
import com.blockchain.console.service.content.AdvertisementManage;
import com.blockchain.console.service.content.entity.Advertisement;
import com.blockchain.console.service.content.entity.AdvertisementRecord;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.http.upload.PartFile;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@MultipartConfig
@Right(id = "C_CONTENT_BANNER_UPDATE", name = "Banner管理-修改")
public class UpdateBanner extends AbstractAdvertiseServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		AdvertisementManage advertisementManage = serviceSession
				.getService(AdvertisementManage.class);
		AdvertisementRecord record = advertisementManage.get(IntegerParser
				.parse(request.getParameter("id")));
		if (record == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		request.setAttribute("record", record);
		forwardView(request, response, getClass());
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		try {
			AdvertisementManage advertisementManage = serviceSession
					.getService(AdvertisementManage.class);
			advertisementManage.update(
					IntegerParser.parse(request.getParameter("id")),
					new Advertisement() {
						@Override
						public Timestamp getUnshowTime() {
							return TimestampParser.parse(request
									.getParameter("unshowTime"));
						}

						@Override
						public String getURL() {
							return request.getParameter("url");
						}

						@Override
						public String getTitle() {
							return request.getParameter("title");
						}

						@Override
						public int getSortIndex() {
							return IntegerParser.parse(request
									.getParameter("sortIndex"));
						}

						@Override
						public Timestamp getShowTime() {
							return TimestampParser.parse(request
									.getParameter("showTime"));
						}

						@Override
						public UploadFile getImage() throws IOException,
								ServletException {
							Part part = request.getPart("image");
							if (part == null || part.getContentType() == null
									|| part.getSize() == 0) {
								return null;
							}
							return new PartFile(part);
						}
						
						@Override
						public SbType getSblx() {
							return EnumParser.parse(SbType.class, request
									.getParameter("sblx"));
						}
					});
			sendRedirect(request, response,
					getController().getURI(request, SearchBanner.class));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}

}
